<?php
// FriendController.php
namespace app\api\controller;
use app\common\model\User;
use app\common\model\Friendship;
use app\common\model\Suggest;
use app\common\controller\Api;
use think\Log;

class Friend extends Api
{
    protected $noNeedLogin = ['addFriend'];
    protected $noNeedRight = '*';
    
    public function _initialize()
    {
      parent::_initialize();
    
      $user = $this->auth->getUser();
      $user && $this->uid = $user->id;
    
    }
    
    public function addSugest(){
        $content = $this->request->post('content');
        $type = $this->request->post('type');
        Suggest::create(['creator_id' => $this->uid, 'content' => $content, 'type' => $type]);
        $this->success('添加建议成功');
    }
    
    // 同意加好友
    public function agreeAdd(){
        $fId = $this->request->post('fId');
        Friendship::where('id',$fId)->setField('status', 'accepted');
        $this->success('添加好友成功');
    }
    
    // 查询请求列表
    public function getAddList(){
        $list = Friendship::alias('f')
            ->join('user u','f.user_id1 = u.id')
            ->where('f.user_id2', $this->uid)->where('f.status', 'pending')->field("u.id,u.nickname,u.avatar,f.id as fId")->select();
        $this->success('查询请求列表成功', $list);
    }
    
    // 查询请求好友数
    public function getNum(){
        $num = Friendship::where('user_id2', $this->uid)->where('status', 'pending')->count();
        $this->success('查询成功', ['num' => $num]);
    }
    
    public function addMyFriend(){
        $fId = $this->request->post('fId');
        // 查询下有没有发过
        $list = Friendship::where('user_id1', $this->uid)->where('user_id2', $fId)->where('status', 'pending')->select();
        if(count($list)){
            $this->error('您已发送好友请求了~');
        }
        $this->addFriend($this->uid, $fId);
        $this->success('请求添加好友成功');
    }
    
    public function addFriend($userId1, $userId2)
    {
        Log::record(34343);
        Log::record($userId1.'----'.$userId2);
        $friendship = new Friendship();
        $friendship->user_id1 = $userId1;
        $friendship->user_id2 = $userId2;
        $friendship->status = 'pending';
        $friendship->created_at = date("Y-m-d H:i:s");
        $friendship->save();
        // 发送通知等逻辑
    }
    
    // 搜索查询好友
    public function getUserList(){
        $keyWord = $this->request->post('keyWord');
        // 先用id匹配
        $uid = $this->uid;
        $list = User::where('id', $keyWord)->field("id,nickname,mobile,avatar")->select();
        
        if(count($list) === 0){
            // 再用手机号匹配
            $list = User::where('mobile', $keyWord)->field("id,nickname,mobile,avatar")->select();
        }
        
        if(count($list) === 0){
            // 再用昵称关键词匹配
            $list = User::whereLike('nickname', "%$keyWord%")->field("id,nickname,mobile,avatar")->select();
        }
        
        foreach ($list as $value){
            // 判断是不是本人
            if($value->id === $uid){
                $value->result = '自己';
            }else{
                // 判断是不是朋友
                $arr = Friendship::where(function($query) use ($value, $uid){
                    $query->where('status', 'accepted');
                    $query->where('user_id1', $value->id);
                    $query->where('user_id2', $uid);
                })
                ->whereOr(function($query) use ( $value, $uid){
                    $query->where('status', 'accepted');
                    $query->where('user_id2', $value->id);
                    $query->where('user_id1', $uid);
                })->select();
                
                if(count($arr)){
                    $value->result = '好友';
                }
            }
            
        }
        $this->success('查询成功', ['list' => $list]);
    }

    public function getFriends()
    {
        $userId = $this->request->post('userId');
        $user = User::find($userId);
       
        $uids1 = \think\Db::name('friendships')
            ->where('status', 'accepted')
            ->where('user_id1', $userId)
            ->column('user_id2');
        $uids2 = \think\Db::name('friendships')
            ->where('status', 'accepted')
            ->where('user_id2', $userId)
            ->column('user_id1');
        $uids = array_merge($uids1, $uids2);
        $friends = User::field('id,username,nickname,avatar,bio')->where('id', 'in', $uids)->select();
        // 处理并返回好友数据
        $this->success('查询成功', ['list' => $friends]);
    }
}